﻿using Niusys.Entities;
using Odn.Dtos;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NHibernate.Linq;

namespace Niusys.Repostories
{
    public interface IProductRepository
    {
        Page<Product> GetProductList();
        Product GetProduct(Guid productId);
        Guid AddProduct(Product product);
        void UpdateProduct(Product product);
        void DeleteProduct(Guid productId);
    }

    public class ProductRepository : BaseRepository, IProductRepository
    {
        public Guid AddProduct(Product product)
        {
            Session.Persist(product);
            Session.Flush();
            return product.ProductId;
        }

        public void DeleteProduct(Guid productId)
        {
            Session.Delete(Session.Load<Product>(productId));
        }

        public Product GetProduct(Guid productId)
        {
            return Session.Get<Product>(productId);
        }

        public Page<Product> GetProductList()
        {
            var list = Session.Query<Product>().ToList();
            return new Page<Product>() { Records = list, Paging = new Paging() { Total = list.Count } };
        }

        public void UpdateProduct(Product product)
        {
            Session.Update(Session.Merge(product));
            Session.Flush();
        }
    }
}
